1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy.
4 : courses/classes/class.Leaf.inc.php
5 : - Modulgruppe: Kurse
6 : - Beschreibung: Klasse fuer Knoten des Baumes.
7 : - Version: 0.3, 12/05/06
8 : - Autor(en): Christoph Gockel <christoph.gockel@mni.fh-giessen.de>
9 : +---------------------------------------------------------------------------+
10 : This program is free software; you can redistribute it and/or
11 : modify it under the terms of the GNU General Public License
12 : as published by the Free Software Foundation; either version 2
13 : of the License, or any later version.
14 : +---------------------------------------------------------------------------+
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 : +--------------------------------------------------------------------------*/
23 :
24 : /**
25 : * <code>Node</code> stellt einen Knoteneintrag im Baum dar.
26 : * @package eStudy.Courses
27 : * @author Christoph Gockel <christoph.gockel@mni.fh-giessen.de>
28 : * @version 0.3, 12/05/06
29 : */
30 :
31 : class Node
32 : {
33 : /** Die Id des Knoteneintrags. */
34 : var $_id;
35 : /** Die parentId des Knoteneintrags. */
36 : var $_parentId;
37 : /** Der Titel des Knoteneintrags. */
38 : var $_caption;
39 : /** Die Beschreibung des Knoteneintrags. */
40 : var $_description;
41 : /** Die weiteren Kinder des Knoteneintrags. */
42 : var $_child;
43 : /** Typ des Knotens. */
44 : var $_type;
45 : /** Typ des Knotens. */
46 : var $_creatorId;
47 :
48 : /**
49 : * Erstellt ein neues Knotenobjekt mit der Id <code>$id</code> und dem Titel/der Beschriftung <code>$caption</code>.
50 : */
51 : function Node($id, $parentId, $caption, $description=NULL, $type=NULL, $creatorId = NULL) {
52 0 : if ( !is_int($id)) {
53 0 : die('Error: id is no integer.');//TODO: bessere Fehlerbehandlung...
54 : }
55 :
56 0 : if ($caption=='') {
57 0 : die('Error: caption should not be empty.');
58 : }
59 :
60 0 : $this->_id = $id;
61 0 : $this->_caption = $caption;
62 0 : $this->_description = $description;
63 0 : $this->_parentId = $parentId;
64 0 : $this->_child = array();// immer ein feld...selbst wenn nur ein Blatt vorhanden ist
65 0 : $this->_type = $type;
66 0 : $this->_creatorId = $creatorId;
67 0 : }
68 :
69 : /**
70 : * Fuegt einen (weiteren) Nachfolger des Knotens ein.
71 : * @param $childref Die Referenz auf das Objekt was "eingehaengt" werden soll.
72 : */
73 : function setChild(&$childref) {
74 0 : $this->_child[] =& $childref;
75 0 : }
76 :
77 : /**
78 : * Gibt die Id des Knoteneintrags zurueck.
79 : */
80 : function getId()
81 : {
82 0 : return $this->_id;
83 : }
84 :
85 : /**
86 : * Gibt die parentId des Knoteneintrags zurueck.
87 : */
88 : function getParentId()
89 : {
90 0 : return $this->_parentId;
91 : }
92 :
93 : /**
94 : * Gibt den Titel des Knoteneintrags zurueck.
95 : */
96 : function getCaption()
97 : {
98 0 : return $this->_caption;
99 : }
100 :
101 : /**
102 : * Gibt die Beschreibung des Knoteneintrags zurück.
103 : */
104 : function getDescription() {
105 0 : return $this->_description;
106 : }
107 :
108 : /**
109 : * Gibt die Kinder/Das Kind des Knoteneintrags zurueck.
110 : */
111 : function getChild()
112 : {
113 0 : return $this->_child;
114 : }
115 :
116 : /**
117 : * Gibt den Typ des Knotens zurück.
118 : */
119 : function getType() {
120 0 : return $this->_type;
121 : }
122 :
123 : /**
124 : * Gibt den Typ des Knotens zurück.
125 : */
126 : function getCreatorId() {
127 0 : return $this->_creatorId;
128 : }
129 :
130 : /**
131 : * Diese Methode zählt die Blätter im aktuellen Knoten
132 : * <strong>und</strong> in <em>allen</em> Unterknoten.
133 : * @access public
134 : * @return int Anzahl der Blätter
135 : */
136 : function getLeafSum() {
137 0 : return $this->countLeafs(true);
138 : }
139 :
140 : /**
141 : * Diese Methode gibt die Anzahl der Blätter im aktuellen Knoten
142 : * zurück.
143 : * @access public
144 : * @return int Die Anzahl der Blätter im Knoten
145 : */
146 : function getLeafCount() {
147 0 : return $this->countLeafs(false);
148 : }
149 :
150 : /**
151 : * Diese Methode zählt die Blätter vom aktuellen Knoten (und den
152 : * Unterknoten, s. Parameter <code>getall</code>).
153 : * @param bool getall Ob Blätter in Unterknoten mitgezählt werden sollen
154 : * @return int Anzahl der Blätter
155 : * @access private
156 : */
157 : function countLeafs($getall) {
158 0 : $leafCount = 0;
159 0 : foreach ($this->_child as $child) {
160 0 : if (is_a($child, 'leaf')) {
161 0 : $leafCount++;
162 0 : } else if(is_a($child, 'leaf_ecom')) {
163 0 : $leafCount++;
164 0 : } else {
165 0 : if ($getall == true) {
166 0 : $leafCount += $child->countLeafs($getall);
167 0 : }
168 : }
169 0 : }
170 0 : return $leafCount;
171 : }
172 :
173 : public function getNodeCount() {
174 0 : $count = 0;
175 0 : foreach ($this->_child as $child) {
176 0 : if($child->getType() == 'node' || $child->getType() == 'node_ecom') {
177 0 : $count++;
178 0 : }
179 0 : }
180 0 : return $count;
181 : }
182 :
183 : /**
184 : * Gibt TRUE zurück falls der aktuelle Knoten, Knoten als Kinder hat.
185 : * Wenn nicht, FALSE.
186 : */
187 : function hasChildNodes() {
188 0 : return (count($this->_child) > 0);
189 : }
190 :
191 : /**
192 : * Die Stringdarstellung des Knoteneintrags.
193 : */
194 : function __toString()
195 : {
196 0 : return "Object Node($this->_id, $this->_caption)";
197 : }
198 : }
|